package com.eascs.crm.nsEditDetail.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.O2oMainMonthInfoEntity;
import com.eascs.crm.nsEditDetail.service.IMainMonthInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;


/**
 * @Description:
 *               <p>
 *               内审信息编辑-- 近六个月营业额情况
 *               </p>
 * @author LMX
 * @date 2017年10月24日 下午1:20:31
 * @version V1.0
 */
@Controller
@RequestMapping("/mainMonthInfoRest")
public class MainMonthInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(MainMonthInfoRestController.class);

	@Reference
	IMainMonthInfoService mainMonthInfoService;

	@Value("${web.crm.url}")
	private String crmUrl;
	
	/**
	 * @Description:
	 *               <p>
	 *               新增/修改
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/updatePage")
	public HtmlRestResult updateIndex(String loanapplyNo) {
		logger.info("近六个月营业额情况查询loanapplyNo：【{}】", loanapplyNo);
		O2oMainMonthInfoEntity entity = mainMonthInfoService.getByLoanNo(loanapplyNo);
		ViewContext.put("view", entity);
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("crmUrl", crmUrl);// 地址选择URL
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/main_month_info_update");
	}

	/**
	 * 
	 * @Description:
	 *               <p>
	 *               保存 新增/修改
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/save")
	public BaseRestResult<String> assetsSave(O2oMainMonthInfoEntity form) {
		O2oMainMonthInfoEntity entity = mainMonthInfoService.getByLoanNo(form.getLoanapplyNo());
		if (entity == null) {
			createO2oMainMonthInfo(form);
			boolean flag = mainMonthInfoService.insert(form);// 新增
			if (flag) {
				return BaseRestResult.info("添加近六个月营业额情况成功").andRefresh();
			} else {
				return BaseRestResult.warn("添加近六个月营业额情况失败").andRefresh();
			}
		} else {
			if (StringUtil.isBlank(form.getId())) {
				return BaseRestResult.warn("更新近六个月营业额情况失败:记录不存在").andRefresh();
			}
			O2oMainMonthInfoEntity info = mainMonthInfoService.getEntityById(form.getId());
			form = modifyO2oMainMonthInfo(info, form);
			boolean flag = mainMonthInfoService.update(form);// 修改
			if (flag) {
				return BaseRestResult.info("修改近六个月营业额情况成功").andRefresh();
			} else {
				return BaseRestResult.warn("修改近六个月营业额情况失败").andRefresh();
			}
		}
	}


	private void createO2oMainMonthInfo(O2oMainMonthInfoEntity info){
		User user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());
		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());
		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
	}

	private O2oMainMonthInfoEntity modifyO2oMainMonthInfo(O2oMainMonthInfoEntity info,O2oMainMonthInfoEntity form){
		User user = UserHolder.getUser();
		form.setModifyDate(new Date());
		form.setModifyId(user.getNo());
		form.setModifier(user.getCname());
		
		form.setCreateId(info.getCreateId());
		form.setCreateDate(info.getCreateDate());
		form.setCreator(info.getCreator());
		return form;
	}
}
